package com.pk.platform.business.charge.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Set;

import com.pk.platform.business.charge.dao.IChargeIndexDao;
import com.pk.platform.business.charge.dao.ITuitionDetailDao;
import com.pk.platform.business.charge.service.IChargeIndexService;
import com.pk.platform.business.charge.vo.ChargeIndexVO;
import com.pk.platform.domain.charge.ChargeIndex;
import com.pk.platform.domain.charge.ChargeTemp;
import com.pk.platform.util.SelectOption;
import com.pk.platform.util.WebUtil;
import com.pk.platform.util.constant.ChargeConstant;
import com.pk.platform.util.constant.Constant;
import com.pk.platform.util.page.ListPage;
import com.pk.platform.util.page.Pager;

public class ChargeIndexServiceImpl implements IChargeIndexService {

	private IChargeIndexDao chargeIndexDao;
	
	private ITuitionDetailDao tuitionDetailDao;
	
	public String addChargeIndex(ChargeIndex ci) {
		String operate = "新增费用指标";
		boolean exist = chargeIndexDao.exist("charge_index", "indexName", ci.getIndexName());
		if(!exist)
		{
			if(ci.getModifyAmount() == null)
			{
				ci.setModifyAmount(1D);
			}
			chargeIndexDao.add(ci);
			chargeIndexDao.addOperateLog(operate, Constant.SUCCESSFUL, null);
			return Constant.SUCCESS;
		}
		chargeIndexDao.addOperateLog(operate, "失败，已存在该指标", null);
		return Constant.EXIST;
	}

	public void delChargeIndex(String id) {
		int count = tuitionDetailDao.indexDetailCount(id);
		ChargeIndex chargeIndex = this.queryChargeIndexById(id);
		chargeIndexDao.addOperateLog("删除费用指标", Constant.SUCCESSFUL, null);
		//删除中间关系
		Set<ChargeTemp> ctSet = chargeIndex.getChargeTempSet();
		for(ChargeTemp ct : ctSet)
		{
			ct.getChargeIndexSet().remove(chargeIndex);
		}
		chargeIndexDao.getHibernateSession().flush();
		if(count > 0)
		{
			chargeIndex.setIsDel(Constant.Y_INT);
		}
		else
		{
			chargeIndexDao.delete(ChargeIndex.class, id);
		}
	}

	public ListPage queryChargeIndex(ChargeIndexVO civo, Pager pager) {
		return chargeIndexDao.queryChargeIndex(civo, pager);
	}

	public ChargeIndex queryChargeIndexById(String id) {
		return chargeIndexDao.get(ChargeIndex.class, id);
	}

	public boolean updateChargeIndex(ChargeIndex ci) {
		ChargeIndex chargeIndex = this.queryChargeIndexById(ci.getId());
		String operate = "修改费用指标";
		boolean flag = chargeIndexDao.existChargeIndexForUpdate(ci.getIndexName(), ci.getId());
		if(!flag)
		{
			chargeIndex.setIndexName(ci.getIndexName());
			chargeIndex.setDescription(ci.getDescription());
			if(ci.getModifyAmount() == null)
			{
				ci.setModifyAmount(1D);
			}
			chargeIndex.setModifyAmount(ci.getModifyAmount());
			chargeIndex.setIndexType(ci.getIndexType());
			chargeIndex.setSort(ci.getSort());
			chargeIndex.setIsOnce(ci.getIsOnce());
			chargeIndexDao.addOperateLog(operate, Constant.SUCCESSFUL, null);
		}
		else
		{
			chargeIndexDao.addOperateLog(operate, "失败，已存在该指标", null);
		}
		return !flag;
	}
	
	public List<ChargeIndex> queryChargeIndexList()
	{
		return chargeIndexDao.queryChargeIndexList();
	}
	
	public List<Map<String, Object>> queryHasSetChargeIndexList(String tempId)
	{
		return chargeIndexDao.queryHasSetChargeIndexList(tempId);
	}
	
	public List<SelectOption> queryIndexTypeList()
	{
		return WebUtil.constructOptions(ChargeConstant.INDEX_TYPE);
	}
	
	
	
	
	
	

	public IChargeIndexDao getChargeIndexDao() {
		return chargeIndexDao;
	}

	public void setChargeIndexDao(IChargeIndexDao chargeIndexDao) {
		this.chargeIndexDao = chargeIndexDao;
	}

	public ITuitionDetailDao getTuitionDetailDao() {
		return tuitionDetailDao;
	}

	public void setTuitionDetailDao(ITuitionDetailDao tuitionDetailDao) {
		this.tuitionDetailDao = tuitionDetailDao;
	}

}
